Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
How to look for an item in DB when the type is String | String[] using typescript union types

I am calling a method sendEmail that sends an email to a single user or a bunch of users(the type of to email is string | string[])

Is there a cleaner/better way to determine if it's an array or a single user so that I can look for it in the DB accordingly? The code looks quite repetitive now.

  public async sendEmail(
    toEmail: string | string[],
  ): Promise<void> {
    if (!Array.isArray(toEmail)) {
      const user = await this.userRepository.getUserByEmail(toEmail);
      await checkIfPaymentMade(user);
    } else {
      for (const email of toEmail) {
       const user = await this.userRepository.getUserByEmail(toEmail);
        await checkIfPaymentMade(user);
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

A few other options come to mind

  1. Extract the common code to its own function. There's still some duplication, but just one line instead of however long the helper function needs to be.
public async sendEmail(
  toEmail: string | string[]
): Promise<void> {
  if (!Array.isArray(toEmail)) {
    await doStuff(toEmail);
  } else {
    for (const email of toEmail) {
      await doStuff(email);
    }
  }
}

private async doStuff(toEmail: string): Promise<void> {
  const user = await this.userRepository.getUserByEmail(toEmail);
  await checkIfPaymentMade(user);
}
  1. Have the body of the function only work with standalone strings. If it's an array, recurse.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  if (Array.isArray(toEmail) {
    for (const email of toEmail) {
      await sendEmail(email)
    }
  } else {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
  1. Have the body of the function only work with arrays. If it's not an array, turn it into one first.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  const emailArray = Array.isArray(toEmail) ? toEmail : [toEmail];

  for (const email of emailArray) {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you just want to keep it DRY, here's an option:

TS Playground link

public async sendEmail (recipients: string | string[]): Promise<void> {
  for (const address of Array.isArray(recipients) ? recipients : [recipients]) {
    const user = await this.userRepository.getUserByEmail(address);
    await checkIfPaymentMade(user);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda